package com.xiegang.serviceMap.remote;

import com.xiegang.internalcommon.constant.AmapConfigConstants;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.internalcommon.response.TerminalResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xieGang
 * @description
 * @date 2023/4/5 11:27
 **/

@Service
public class TerminalClient {

    @Value("${Amap.key}")
    private String key;

    @Value("${Amap.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult<TerminalResponse> add(String name,String desc){

        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.Terminal_ADD_URL);
        url.append("?");
        url.append("key=");
        url.append(key);
        url.append("&");
        url.append("sid=");
        url.append(sid);
        url.append("&");
        url.append("name=");
        url.append(name);
        url.append("&");
        url.append("desc=");
        url.append(desc);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(),null, String.class);

        String body = forEntity.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);
        return ResponseResult.success(terminalResponse);
    }

    public ResponseResult<List<TerminalResponse>> aroundSearch(String center, Integer radius) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.AROUND_SEARCH_URL);
        url.append("?");
        url.append("key=");
        url.append(key);
        url.append("&");
        url.append("sid=");
        url.append(sid);
        url.append("&");
        url.append("center=");
        url.append(center);
        url.append("&");
        url.append("radius=");
        url.append(radius);

        System.out.println("终端搜索请求："+url.toString());
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(),null, String.class);
        System.out.println("终端搜索请求："+forEntity.toString());

        String body = forEntity.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");

        List<TerminalResponse> terminalResponseList = new ArrayList<>();
        JSONArray results = data.getJSONArray("results");
        for(int i = 0;i<results.size();i++){
            TerminalResponse terminalResponse = new TerminalResponse();
            JSONObject jsonObject = results.getJSONObject(i);

            String carId = jsonObject.getString("desc");
            String tid = jsonObject.getString("tid");

            JSONObject location = jsonObject.getJSONObject("location");
            String longitude = location.getString("longitude");
            String latitude = location.getString("latitude");

            terminalResponse.setTid(tid);
            terminalResponse.setCarId(Long.valueOf(carId));
            terminalResponse.setLongitude(longitude);
            terminalResponse.setLatitude(latitude);
            terminalResponseList.add(terminalResponse);
        }


        return ResponseResult.success(terminalResponseList);
    }
}
